extends Node2D
class_name projectile

@export var flying_speed=300;
@export var lifetime = 3.0  # 子弹存活时间(秒)

var damage
var knockback = 0.0
var flying_direction:Vector2
var timer: float = 0.0

func set_up(flying_direction:Vector2):
	self.flying_direction = flying_direction.normalized()
	rotate(flying_direction.angle())

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass
	# 添加Area2D检测碰撞
	#if !has_node("HitBox"):
		#var hitbox = Area2D.new()
		#hitbox.name = "HitBox"
		#var collision = CollisionShape2D.new()
		#collision.shape = CircleShape2D.new()
		#collision.shape.radius = 5  # 根据实际子弹大小调整
		#hitbox.add_child(collision)
		#add_child(hitbox)
		#hitbox.connect("body_entered", _on_body_entered)

func set_damage(damage):
	self.damage=damage

func get_damage():
	return damage

func set_knockback(value):
	self.knockback = value
	
func get_knockback():
	return knockback

# 碰撞检测回调
#func _on_body_entered(body) -> void:
	#if body.has_method("take_damage"):
		#body.take_damage(1)  # 默认伤害值为1
	#queue_free()  # 碰撞后销毁子弹

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	global_position += flying_direction * flying_speed * delta
	
	# 处理自动消失
	timer += delta
	if timer >= lifetime:
		queue_free()
	
